package com.songlanyun.modules.distributionconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.distributionconfig.model.DistributionShopProxyConfigDTO;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.distributionconfig.dao.DistributionShopProxyConfigDao;
import com.songlanyun.modules.distributionconfig.entity.DistributionShopProxyConfig;
import com.songlanyun.modules.distributionconfig.service.DistributionShopProxyConfigService;
import org.springframework.transaction.annotation.Transactional;


@Service("distributionShopProxyConfigService")
public class DistributionShopProxyConfigServiceImpl extends ServiceImpl<DistributionShopProxyConfigDao, DistributionShopProxyConfig> implements DistributionShopProxyConfigService {

    @Override
    public DistributionShopProxyConfig create(DistributionShopProxyConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public DistributionShopProxyConfig modify(DistributionShopProxyConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DistributionShopProxyConfig> page = this.page(
                new Query<DistributionShopProxyConfig>().getPage(params),
                new QueryWrapper<DistributionShopProxyConfig>()
        );

        return new PageUtils(page);
    }

    /**
     * 查看平台商家代销商品小程序分佣配置
     * @param shopId
     * @return
     */
    @Override
    public DistributionShopProxyConfig getByShop(Long shopId) {
        return this.getOne(new LambdaQueryWrapper<DistributionShopProxyConfig>()
        .eq(DistributionShopProxyConfig::getShopId,shopId)
        .last("limit 1"));
    }

    /**
     * 设置平台商家代销商品小程序分佣配置
     * @param shopId
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public DistributionShopProxyConfig set(Long shopId, DistributionShopProxyConfigDTO dto) {
        DistributionShopProxyConfig config = this.getByShop(shopId);
        if(config==null)
        {
            DistributionShopProxyConfig distributionShopProxyConfig = new DistributionShopProxyConfig();
            distributionShopProxyConfig.setShopId(shopId);
            distributionShopProxyConfig.setOpen(dto.getOpen());
            distributionShopProxyConfig.setProxyGoodsRule(dto.getProxyGoodsRule());
            this.create(distributionShopProxyConfig);
            return distributionShopProxyConfig;
        }
        else
        {
            config.setOpen(dto.getOpen());
            config.setProxyGoodsRule(dto.getProxyGoodsRule());
            this.modify(config);
            return config;
        }
    }

}